package com.jair.crawler.opm;

import java.io.IOException;

import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;

/**
 * 用于计算循环次数
 * @author wenjie
 *
 */
public class CountParsr {
	private int currentRemain = 0;
	private int countRemain = 0;
	
	public void initNum(int i){
		this.countRemain = 0;
		this.currentRemain = i;
	}
	public Integer  getSqlCount(HtmlPage page,String sql) throws IOException{
		  HtmlInput dba = page.getElementByName("SqlQuery[schema]");
	         dba.setValueAttribute("10.136.4.47:3334|bpm");
	         HtmlTextArea sqlTextArea = page.getElementByName("SqlQuery[sql]");
	         //拼接sql
	         StringBuffer sqlBuffer = new StringBuffer("select count(1) from (").append(sql).append(") xx_xx_1");
	         sqlTextArea.setText(sqlBuffer.toString());
	         HtmlElement Querybtn = (HtmlElement) page.getElementById("processbutton");
	         HtmlPage ret = Querybtn.click();
	         DomNode node = ret.querySelector(".sql-result");
	         HtmlTable table =  (HtmlTable) node.getByXPath("//table").get(0);
	        return Integer.valueOf(table.getCellAt(1, 0).getTextContent()) ;
	}
}
